//
//  JCLabelView.swift
//  JCTest3
//
//  Created by Chen on 11/10/2022.
//

import Cocoa

// MacOS app中没有UILabel或者是NSLabel， 用UITextField代替
class JCLabelView: JCBaseBottomView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Drawing code here.
//        self.setupViewBackgroundColor(color: NSColor(red: 0.0, green: 0.8, blue: 0.8, alpha: 1.0))
        self.createViews()
    }
    
}

// 创建视图相关
extension JCLabelView {
    
    func createViews() {
        
        // 随便创建一个文本框，实现label控件
        if #available(OSX 10.12, *) {
            let label1 = NSTextField(labelWithString: "This is a label")
            label1.frame = NSRect(x: 12, y: 12, width: 200, height: 60)
            label1.isEditable = false
    //        label1.stringValue = "This is a label"
            label1.font = NSFont.systemFont(ofSize: 16)
            label1.textColor = .blue
            label1.alignment = .center
            label1.wantsLayer = true
            label1.layer?.backgroundColor = NSColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0).cgColor
            
            self.addSubview(label1)
        } else {
            // Fallback on earlier versions
        }
        
        // 创建一个自定义的文字居中的label
        let customLabel = NSTextField(labelWithString: "文字是居中的不可编辑不可滚动的")
        customLabel.frame = NSRect(x: 12, y: 90, width: 200, height: 80)
        let cell = JCCustomTextFieldCell(textCell: "文字是居中的不可编辑不可滚动的文字是居中的不可编辑不可滚动的")
        cell.isEditable = false
        customLabel.cell = cell
        customLabel.wantsLayer = true
        customLabel.layer?.backgroundColor = NSColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0).cgColor
        self.addSubview(customLabel)
        
        //
        let customLabel1 = NSTextField(labelWithString: "文字是居中的不可编辑不可滚动的")
        customLabel1.frame = NSRect(x: 12, y: 200, width: 200, height: 80)
        let cell1 = JCCustomTextFieldCell(textCell: "文字是居中的可以编辑不可滚动的文字是居中的可以编辑不可滚动的")
        cell1.isEditable = true
        customLabel1.cell = cell1
        customLabel1.wantsLayer = true
        customLabel1.layer?.backgroundColor = NSColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0).cgColor
        self.addSubview(customLabel1)
        
        let customLabel2 = NSTextField(labelWithString: "文字是居中的不可编辑不可滚动的")
        customLabel2.frame = NSRect(x: 12, y: 300, width: 200, height: 80)
        let cell2 = JCCustomTextFieldCell(textCell: "文字是居中的可以编辑可以滚动的文字是居中的可以编辑可以滚动的")
        cell2.isEditable = true
        cell2.isScrollable = true
        cell2.font = NSFont.systemFont(ofSize: 36)
        customLabel2.cell = cell2
        customLabel2.wantsLayer = true
        customLabel2.layer?.backgroundColor = NSColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0).cgColor
        self.addSubview(customLabel2)
        
    }
    
}


// MARK: 自定义NSTextFieldCell
class JCCustomTextFieldCell: NSTextFieldCell {
    
    override init(textCell string: String) {
        super.init(textCell: string)
        self.isScrollable = false
        self.drawsBackground = true
        self.backgroundColor = .white
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension JCCustomTextFieldCell {
    private func adjustedFrameToVerticallyCenter(frame: NSRect) -> NSRect {
        // 当前字体所有文字基准线以上的最高y坐标
        let ascender = font?.ascender ?? 0.0
        // 基准线以下的最低y坐标，一般为负数
        let descender = font?.descender ?? 0.0
        // ceilf不小于参数的最小整数
        print("frame:\(frame), ascender: \(ascender), descender: \(descender)")
        let offset = ceilf(Float(NSHeight(frame)/2 - ascender - descender))
        // 以原来frame的中心原点为中心点，向x和y方向分别延伸
        return NSInsetRect(frame, 0, CGFloat(offset))
    }
}

extension JCCustomTextFieldCell {
    override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {
        let frame = adjustedFrameToVerticallyCenter(frame: rect)
        super.edit(withFrame: frame, in: controlView, editor: textObj, delegate: delegate, event: event)
    }
    
    override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {
        let frame = adjustedFrameToVerticallyCenter(frame: rect)
        super.select(withFrame: frame, in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)
    }
    
    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        let frame = adjustedFrameToVerticallyCenter(frame: cellFrame)
        super.drawInterior(withFrame: frame, in: controlView)
    }
}
